package ee.ut.veebirakendus.bot;

import org.apache.log4j.Logger;

import ee.ut.veebirakendus.servlet.PokerServlet;
import ee.ut.veebirakendus.servlet.PokerServlet.ChatSocket;

public class ChatThread implements Runnable {

	private Thread chat;
	private ChatSocket chatSocket;
	Logger log;

	public ChatThread(ChatSocket chatSocket) {
		chat = new Thread(this);
		this.chatSocket = chatSocket;
		log = Logger.getLogger(PokerServlet.SERVLET_NAME);
		log.info("Chat thread initialized");
		chat.start();
	}

	@Override
	public void run() {
		while (!chat.isInterrupted()) {
			try {
				Thread.sleep(5000);
				chatSocket.sendMessage();
			} catch (InterruptedException e) {
				stop();
				log.debug("Sleep interrupted, thread stopped");
			}
		}
	}

	public void stop() {
		if (chat != null)
			chat.interrupt();
	}
}
